﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class ManagerMainAR : System.Web.UI.Page
{

    public Persons mobjPersons
    {
        get
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPersons"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjPersons"].ToString()] = value;
        }
    }



    public List<Persons> lstGridPersons
    {
        get
        {
            if (ViewState["lstGridPersons"] == null)
            {
                ViewState["lstGridPersons"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["lstGridPersons"].ToString()] == null)
            {
                Session[ViewState["lstGridPersons"].ToString()] = new List<Persons>();

            }

            return (List<Persons>)Session[ViewState["lstGridPersons"].ToString()];

        }
        set
        {
            if (ViewState["lstGridPersons"] == null)
            {
                ViewState["lstGridPersons"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["lstGridPersons"].ToString()] = value;
        }  



    }



    protected void Page_Load(object sender, EventArgs e)
    {
        Persons objPersons = GlobalVars.getCurrentPersonAR();

        mobjPersons = objPersons;

        checkSecurityAndCustomizePage(objPersons);
       
        if (!Page.IsPostBack)
        {
            bindGrid();
        }

    }



    /// <summary>
    /// this function will bind the grid with the persons
    /// </summary>
    protected void bindGrid()
    {

        mobjPersons.clearchilds();
        List<Persons> lstPersons =   mobjPersons.lstAllEmployees;

        lstGridPersons = lstPersons;
        grdUsers.DataSource = lstPersons;
        grdUsers.DataBind();
    }



    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check



        if ((!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Manager_page)) && currentPerson.lstAddEmployees.Count == 0)
            Response.Redirect("LoginAR.aspx");




    }
    
   




    




    


    protected void grdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {




        long DeletedEmployeeID =  long.Parse(grdUsers.DataKeys[e.RowIndex]["GenID"].ToString());


        ManagerEmployees me = new ManagerEmployees();
        me.EmployeeID = DeletedEmployeeID;
        me.ManagerID = mobjPersons.GenID;

        ManagerEmployeesDB medb = new ManagerEmployeesDB (ConnectionStrings.LMS);

        List<ManagerEmployees> lstNew = medb.Search(me);

        if (lstNew.Count == 0)
        {
            lblError.Text = "خطأ لا يوجد موظفين لازالتهم من صلاحياتك.";
            return;
        }



        medb.Delete(lstNew[0]);


        bindGrid();
        



    }

   
    
    
    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUsers.DataSource = lstGridPersons;
        grdUsers.PageIndex = e.NewPageIndex;
        grdUsers.DataBind();

    }
    
    
    
    protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            long EmployeeID = long.Parse(grdUsers.DataKeys[e.Row.RowIndex]["GenID"].ToString());

            Persons prNew = new Persons();
            prNew.GenID = EmployeeID;

            if (mobjPersons.lstEmployees.Contains(prNew))
            {
                LinkButton lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");
                lnkDelete.Visible = false;

            }

        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        mobjPersons.clearchilds();
        List<Persons> lstPersons = mobjPersons.lstAllEmployees;

        lstPersons = new List<Persons>(from obj in lstPersons where obj.Name_EN.ToLower().Contains(txtSearch.Text.ToLower())
                                           || obj.MiddleName_EN.ToLower().Contains(txtSearch.Text.ToLower())
                                           || obj.FamilyName_EN.ToLower().Contains(txtSearch.Text.ToLower())
                                           || obj.Name_AR.ToLower().Contains(txtSearch.Text.ToLower())
                                           || obj.MiddleName_AR.ToLower().Contains(txtSearch.Text.ToLower())
                                           || obj.FamilyName_AR.ToLower().Contains(txtSearch.Text.ToLower())
                                           || obj.Badge.ToLower().Contains(txtSearch.Text.ToLower())
                                           
                                           select obj
                                                                                     
                                           );


        lstGridPersons = lstPersons;
        grdUsers.DataSource = lstPersons;
        grdUsers.DataBind();
    }
    protected void btnDelegateSelected_Click(object sender, EventArgs e)
    {
        List<Persons> lstPersons = new List<Persons> ();
        PersonsDB objPersonsDB = new PersonsDB (ConnectionStrings.LMS);
        foreach (GridViewRow objRow in grdUsers.Rows)
        {
            if (objRow.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkSelectEmployee = objRow.FindControl("chkSelectEmployee") as CheckBox;
                if (chkSelectEmployee.Checked)
                {
                    long PersonID = long.Parse(grdUsers.DataKeys[objRow.RowIndex]["GenID"].ToString());


                    List<Persons> lstOnePerson = objPersonsDB.getPersonsByGenID(PersonID);

                    lstPersons.AddRange(lstOnePerson);
                }
            }
        }


        if (lstPersons.Count == 0)
        {
            lblError.Text = "يجب اختيار موظف واحد على الاقل لاعطاء صلاحيتهم لمدير اخر.";
            return;
        }


        string strPageID = System.Guid.NewGuid().ToString();

        Session[strPageID] = lstPersons;

        Response.Redirect("AssignToAR.aspx?randid=" + strPageID);



    }
    
    protected void btnDelegateAll_Click(object sender, EventArgs e)
    {
        if (lstGridPersons.Count == 0)
        {
            lblError.Text = "يجب اختيار موظف واحد على الاقل لاعطاء صلاحيتهم لمدير اخر.";
            return;
        }
        
        string strPageID = System.Guid.NewGuid().ToString();
        
        Session[strPageID] = lstGridPersons;


        Response.Redirect("AssignToAR.aspx?randid=" + strPageID);


    }
    protected void btnUnDelegate_Click(object sender, EventArgs e)
    {
        Response.Redirect("UnAssignFromAR.aspx");
    }
}
